

<%@page contentType="text/html" pageEncoding="UTF-8" %>
<%@ page import="java.io.*" %>
<%@ page import="java.sql.*"%>

<%
    String saveFile="";
    String contentType= request.getContentType();
    System.out.println(contentType);

    if((contentType != null) && (contentType.indexOf("multipart/form-data") >= 0))
        {
        System.out.println("Da qua if");
            DataInputStream in = new DataInputStream(request.getInputStream());
            int formDataLenght = request.getContentLength();
            byte dataBytes[] = new byte [formDataLenght];
            int byteRead = 0;
            int totalBytesRead = 0;
            while(totalBytesRead < formDataLenght)
                {
                    byteRead = in.read(dataBytes, totalBytesRead, formDataLenght);
                    totalBytesRead += byteRead;
                }
            
            String file = new String(dataBytes);
            saveFile = file.substring(file.indexOf("filename=\"") + 10);
            saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
            saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1, saveFile.indexOf("\""));
            int lastIndex = contentType.lastIndexOf("=");
            String boundary = contentType.substring(lastIndex + 1, contentType.length());
            int pos;
            pos = file.indexOf("filename=\"");
            pos = file.indexOf("\n", pos) + 1;
            pos = file.indexOf("\n", pos) + 1;
            pos = file.indexOf("\n", pos) + 1;
            int boundaryLocation  = file.indexOf(boundary,pos) - 4;
            int startPos = ((file.substring(0, pos)).getBytes()).length;
            int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length;
            File ff = new File(saveFile);
            FileOutputStream fileOut = new FileOutputStream(ff);
            fileOut.write(dataBytes, startPos, (endPos - startPos));
            fileOut.flush();
            fileOut.close();
            
            %>
            <br>
            <table border="2"><tr><td><b>You have successfully upload the file: </b>
            <% out.println(saveFile);%></td></tr></table>
            <%
            System.out.println("bat dau con");
            Connection connection = null;
            String ConnectionURL = "jdbc:sqlserver://localhost:1433;databaseName=test";
            ResultSet rs = null;
            PreparedStatement psmnt = null;
            FileInputStream fis;
            System.out.println("qua con1");
            try{
                Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
                connection = DriverManager.getConnection(ConnectionURL, "sa","123");
                System.out.println("qua con2");
                File f = new File(saveFile);
                
                System.out.println("bat dau insert");
                psmnt = connection.prepareStatement("insert into files(file_data) values(?)");
                System.out.println("qua insert");
                fis = new FileInputStream(f);
                psmnt.setBinaryStream(1, (InputStream)fis, (int)(f.length()));
                int s = psmnt.executeUpdate();
                System.out.println("qua update");
                if(s>0)
                    {
                        System.out.println("Upload Successfully !");
                    }
                else
                    {
                        System.out.println("Error!");
                    }
                }catch(Exception e)
            {e.printStackTrace();}
        }
%>
<!--
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
    </head>
    <body>
        <h1>Hello World!</h1>
    </body>
</html>-->
